/** 
 * @Title: MixDao.java 
 * @Package com.zizo.autohome.sta.mix 
 * @Description: TODO
 * @author HeYan
 * @date 2016年10月27日 上午11:21:38 
 */
package com.zizo.autohome.sta.mix;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @ClassName: MixDao
 * @Description: TODO
 * @author HeYan
 * @date 2016年10月27日 上午11:21:38
 * 
 */
public class MixDao implements Runnable {
	private Connection conn;
	private int brdid;
	private int cid;
	private int tyear;
	private float alcj;
	private int ocounts;

	public MixDao(Connection conn, int brdid, int cid, int tyear, float alcj, int ocounts) {
		super();
		this.conn = conn;
		this.brdid = brdid;
		this.cid = cid;
		this.tyear = tyear;
		this.alcj = alcj;
		this.ocounts = ocounts;
	}

	public float getPro() {
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT COUNT(price_mid) FROM other_price WHERE other_price.price_cid=" + brdid
				+ " AND YEAR(other_price.price_btime)=" + tyear + ";";
		float pro = 0;
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int temp = rs.getInt(1);
				pro = ocounts / (float) temp;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pro;
	}

	public String getName() {
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT bp_name,bp_brname FROM autohome_other.other_core_basicparam where bp_mid=" + cid
				+ " and bp_brdid=" + brdid;
		String res = "";
		try {
			st = conn.createStatement();
			rs = st.executeQuery(sql);
			if (rs.next()) {
				String bp_name = rs.getString(1);
				String bp_brname = rs.getString(2);
				res = bp_name + "%%" + bp_brname;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return res;
	}

	public void update() {
		PreparedStatement ps = null;
		String sql = "INSERT INTO carvoice_biz.biz_mix1(mix_brdid,mix_brdname,mix_cid,mix_cname,mix_year,mix_lcj,mix_cnt,mix_pro) VALUES(?,?,?,?,?,ROUND(?,2),?,ROUND(?*100,2));";
		String name = getName();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, brdid);
			ps.setString(2, name.split("%%")[1]);
			ps.setInt(3, cid);
			ps.setString(4, name.split("%%")[0]);
			ps.setInt(5, tyear);
			ps.setFloat(6, alcj);
			ps.setInt(7, ocounts);
			ps.setFloat(8, getPro());
			ps.executeUpdate();
			System.out.println(brdid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void run() {
		update();
	}
}
